//
//  SEGHouseUserListViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/5.
//

#import "SEGHouseUserListViewModel.h"

@implementation SEGHouseUserListViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}
- (void)racInit {
    
    @weakify(self);
    
    _requestDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            @strongify(self)
            
            [self.requestManager requestUserListWithHouseId:self.houseId finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                
                if (isSuccessed) {
                    self.dataArray = self.requestManager.userInfoArray;
                    [subscriber sendNext:nil];
                }else {
                    [self.messageDataSubject sendNext:errorMessage];
                }
                [subscriber sendCompleted];
                
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _delHouseUserDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSNumber*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            @strongify(self)
            
            if (input) {
                
                SEGUserInfoModel *model = self.dataArray[input.integerValue];
                
                [self.requestManager requestDelHouseUserWithHouseId:model.houseId userId:model.userId finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                    if (isSuccessed) {
                        [self.messageDataSubject sendNext:@"已删除"];
                        [subscriber sendNext:nil];
                    }else {
                        [self.messageDataSubject sendNext:errorMessage];
                    }
                    [subscriber sendCompleted];
                }];
            }
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods

#pragma mark - Getter
- (SEGUserHouserRequestManager *)requestManager
{
    if (!_requestManager) {
        _requestManager = [[SEGUserHouserRequestManager alloc] init];
    }
    return _requestManager;
}

-(RACSubject *)messageDataSubject{
    if (!_messageDataSubject) {
        _messageDataSubject = [RACSubject subject];
    }
    return _messageDataSubject;
}

#pragma mark - Stter
@end
